const {getList, getDetail, newBlog, updateBlog, delBlog} = require('../controller/blog')
const {SuccessModel, ErrorModel} = require('../model/resModel')

//  统一登录验证
const loginCheck = (req) => {
    if(!req.session.username) {
        return Promise.resolve(new ErrorModel('尚未登录'))
    }
}

const handleBlogRouter = (req, res) => {
    const {method, url} = req
    const path = url.split('?')[0]
    const id = req.query.id
    // 获取博客列表
    if(method === 'GET' && path === '/api/blog/list') {
        const author = req.query.author || ''
        const keyword = req.query.keyword || ''
        let result = getList(author, keyword)
        return result.then(listData => {
            return new SuccessModel(listData, '成功')
        })
        
    }

    // 博客详情
    if(method === 'GET' && path === '/api/blog/detail'){
        
        const result =  getDetail(id)
        return result.then(detailData => {
            return new SuccessModel(detailData)
        })
        
    }
    
    // 新建一篇博客
    if(method === 'POST' && path ==='/api/blog/new'){
        const loginCheckResult = loginCheck(req)
        if(loginCheckResult){
            return loginCheckResult
        }
        req.body.author = req.session.username
        const result = newBlog(req.body)
        return result.then(data => {
            return new SuccessModel(data)
        })
    }
    
    // 更新一篇博客
    if(method === 'POST' && path ==='/api/blog/update'){
        const loginCheckResult = loginCheck(req)
        if(loginCheckResult){
            return loginCheckResult
        }
        const result = updateBlog(id, req.body)
        return result.then(val => {
            if(val){  
                return new SuccessModel({},'更新成功')
             } else {
                return new ErrorModel(null, '更新博客失败')
            } 
        })
    }

    // 删除一篇博客
    if(method === 'POST' && path ==='/api/blog/delete'){
        const loginCheckResult = loginCheck(req)
        if(loginCheckResult){
            return loginCheckResult
        }
        const author = req.session.username
        const result=delBlog(id, author)
        return result.then(val => {
            if(val){
                return new SuccessModel({},'删除成功')
            } else {
                return new ErrorModel(null, '删除失败')
            }
        })
    }
}
module.exports = handleBlogRouter